﻿using System;
using System.ComponentModel;
using System.Data;
using System.Data.Objects;
using System.Linq;
using System.Windows.Forms;

namespace RedRiver.Framework.XtraCommon.FormHelper{
    public class EntityDataContext<TContext> : IEntityDataContext<TContext>
        where TContext : ObjectContext{
        private TContext _context;

        public EntityDataContext(){}

        public EntityDataContext(TContext context){
            _context = context;
        }

        public EntityDataContext(IEntityDataContext<TContext> context) : this(context.Context){}

        #region Implementation of IEntityDataContext<TContext>

        public TContext Context{
            get { return _context ?? (_context = Activator.CreateInstance<TContext>()); }
            set { _context = value; }
        }

        public bool HasEntityChanged{
            get{
                return Context.ObjectStateManager.GetObjectStateEntries(EntityState.Added | EntityState.Deleted |
                                                                 EntityState.Modified).Count() > 0;
            }
        }

        public Action<TContext> OnLoadData{
            get;
            set;
        }

        public Action<TContext> OnUpdateData{
            get;
            set;
        }

        public Action<TContext> OnRefreshData{
            get;
            set;
        }

        #endregion

        #region IEntityDataContext<TContext> Members

        public void LoadData(){
            if (OnLoadData != null){
                OnLoadData(Context);
            }
        }

        public void UpdateData(){
            if (OnUpdateData != null){
                OnUpdateData(Context);
            }
        }

        public void RefreshData(){
            if (OnRefreshData != null){
                OnRefreshData(Context);
            }
        }

        ObjectContext IEntityDataContext.Context{
            get { return _context ?? (_context = Activator.CreateInstance<TContext>()); }
            set { _context = (TContext) value; }
        }

        #endregion
        }

    public interface IDataContext{
        void LoadData();

        void UpdateData();

        void RefreshData();
    }

    public interface IEntityDataContext : IDataContext{
        ObjectContext Context{
            get;
            set;
        }
        /// <summary>
        /// 判断当前编辑的数据内容是否改变
        /// </summary>
        bool HasEntityChanged{
            get;
        }
    }

    public interface IEntityDataContext<TContext> : IEntityDataContext where TContext : ObjectContext{
        new TContext Context{
            get;
        }

        Action<TContext> OnLoadData{
            get;
            set;
        }

        Action<TContext> OnUpdateData{
            get;
            set;
        }

        Action<TContext> OnRefreshData{
            get;
            set;
        }
    }
}